<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\JobRequest;
use App\Models\Job;
use App\Transformers\JobTransformer;
use Illuminate\Http\Request;

class JobController extends Controller
{
    public function index(Request $request, Job $job)
    {
        $job = $job->newQuery();
        if ($request->input('name')) {
            $job->where('name', $request->input('name'));
        }
        if ($request->input('project_id')) {
            $job->where('project_id', $request->input('project_id'));
        }
        if ($request->input('school') || $request->input('school') === 0 || $request->input('school') === '0') {
            $job->where('school', $request->input('school'));
        }
        if ($request->input('sex') || $request->input('sex') === 0 || $request->input('sex') === '0') {
            $job->where('sex', $request->input('sex'));
        }

        $jobs = $job->orderBy('created_at', 'desc')->paginate(20);

        return $this->response->paginator($jobs, new JobTransformer());
    }

    public function store(JobRequest $request)
    {
        $job = Job::create([
            'name' => $request->name,
            'project_id' => $request->project_id,
            'school' => $request->school,
            'post' => $request->post,
            'sex' => $request->sex,
            'education' => $request->education,
            'specialty1' => '',
            'specialty2' => '',
            'specialty3' => '',
            'number' => $request->number,
        ]);

        return $this->response->item($job, new JobTransformer())
            ->setStatusCode(201);
    }

    public function update(JobRequest $request, Job $job)
    {
        $job->update([
            'name' => $request->name,
            'project_id' => $request->project_id,
            'school' => $request->school,
            'post' => $request->post,
            'sex' => $request->sex,
            'specialty1' => '',
            'specialty2' => '',
            'specialty3' => '',
            'number' => $request->number,
        ]);

        return $this->response->item($job, new JobTransformer());
    }

    public function delete(Job $job)
    {
        $job->delete();

        return $this->response->noContent();
    }
}
